﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Options.IBClient.Test
{
    delegate void SetTextCallback(string text);
    public partial class Test : Form
    {
        private static int NextOrderId = 0;
        private static TWSLib.IContract contract;
        public AxTWSLib.AxTws tws = new AxTWSLib.AxTws();

        public Test()
        {
            InitializeComponent();
            tws.CreateControl();


            // client.ThrowExceptions = true;

            tws.tickPrice += client_TickPrice;
            tws.tickSize += client_TickSize;
            tws.errMsg += client_Error;
            tws.currentTime += client_CurrentTime;
            tws.tickOptionComputation += client_TickOptionComputation;

        }

        private void client_TickSize(object sender, AxTWSLib._DTwsEvents_tickSizeEvent e)
        {
           this.SetTWSErrorMsg("Tick Size: " + e.size + " Tick Type: " + (((TickType)e.tickType).ToString()));
        }

        private void client_Error(object sender, AxTWSLib._DTwsEvents_errMsgEvent e)
        {
            this.SetTWSErrorMsg("Error: " + e.errorMsg);
        }

        private void client_TickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
        {
            this.SetTWSErrorMsg("Price: " + e.price.ToString() + " Tick Type: " + (((TickType)e.tickType).ToString()));
        }
        private  void client_CurrentTime(object sender, AxTWSLib._DTwsEvents_currentTimeEvent e)
        {
            //SetTWSErrorMsg("Time: " + e.time.ToString());
            this.SetTWSErrorMsg("TWS: Connected at (UTC Time) " + e.time.ToString());

        }
        private void client_TickOptionComputation(object sender, AxTWSLib._DTwsEvents_tickOptionComputationEvent e)
        {
            this.SetTWSErrorMsg("Delta " + e.delta.ToString());
        }
        private void button1_Click(object sender, EventArgs e)
        {
           
            resultBox.Text = "Connecting to IB.";
            tws.connect("127.0.0.1", 7496, 2);
            contract = tws.createContract();
            contract.symbol = "SPY";
            contract.secType = "OPT";
            contract.expiry = "20130125";
            contract.strike = 150;
            contract.right = "C";
            contract.exchange = "SMART";
            contract.currency = "USD";

            tws.reqCurrentTime();
            tws.reqMktDataEx(14, contract, null, 0);

            //client.RequestMarketDepth(15, Google, 5);
            //client.RequestRealTimeBars(16, Google, 5, RealTimeBarType.Trades, false);
            //client.RequestMarketData(17, EUR, null, false, false);

            //Order BuyContract = new Order();
            //BuyContract.Action = ActionSide.Buy;
            //BuyContract.OutsideRth = false;
            //BuyContract.LimitPrice = 560;
            //BuyContract.OrderType = OrderType.Limit;
            //BuyContract.TotalQuantity = 1;
            //client.PlaceOrder(503, TF, BuyContract);

            //client.RequestExecutions(34, new ExecutionFilter());

            //client.RequestAllOpenOrders();
           
        }

        private void SetTWSErrorMsg(string text)
        {
            if (this.resultBox.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetTWSErrorMsg);
                if (IsHandleCreated)
                    this.Invoke(d, new object[] { text });
            }
            else
            {
                this.resultBox.AppendText(text);
            }
        }

    }
}
